using Crux.Core.Dapper.SqlGenerator;

namespace Crux.Core.Dapper;
public partial class QueryRepository<TContext> where TContext : DapperContext
{
    /// <inheritdoc/>
    public bool Any<TEntity>(
        Expression<Func<TEntity, bool>> predicate,
        bool ignoreFilter = false,
        Action<SqlGenerator<TEntity>>? configure = null) where TEntity : class
    {
        var generator = GetSqlGenerator(configure);

        var sqlQuery = generator.GetAny(predicate, ignoreFilter);

        var exists = QueryFirstOrDefault<bool>(sqlQuery.GetSql(), sqlQuery.Param);

        return exists;
    }
    /// <inheritdoc/>
    public async Task<bool> AnyAsync<TEntity>(
        Expression<Func<TEntity, bool>> predicate,
        bool ignoreFilter = false,
        Action<SqlGenerator<TEntity>>? configure = null,
        CancellationToken cancellationToken = default) where TEntity : class
    {
        var generator = GetSqlGenerator(configure);

        var sqlQuery = generator.GetAny(predicate, ignoreFilter);

        var exists = await QueryFirstOrDefaultAsync<bool>(sqlQuery.GetSql(), sqlQuery.Param, cancellationToken).ConfigureAwait(false);

        return exists;
    }
}